package com.kasax.test.bc.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.kasax.hilog.Logger
import com.kasax.test.bc.R
import com.kasax.test.bc.databinding.LayoutItemTestBinding

class TestAdapter(val datas: List<String>) : RecyclerView.Adapter<TestAdapter.TestViewHolder>() {
    override fun onCreateViewHolder(
        parent: ViewGroup, viewType: Int
    ): TestViewHolder {
        val itemBinding: LayoutItemTestBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context), R.layout.layout_item_test, parent, false
        )
        return TestViewHolder(itemBinding)
    }

    override fun onBindViewHolder(holder: TestViewHolder, position: Int) {
        Logger.log("onBindViewHolder $position")
        holder.bind(datas[position])
    }

    override fun getItemCount(): Int = datas.size

    class TestViewHolder(val itemViewBinding: LayoutItemTestBinding) : RecyclerView.ViewHolder(itemViewBinding.root) {
        fun bind(url: String) {
            Logger.log("TestViewHolder bind url: $url")
            itemViewBinding.itemUrl = url
            itemViewBinding.executePendingBindings()
        }
    }
}